﻿using System;
using System.Net;
using System.Threading.Tasks;
using System.Web.Mvc;
using Ext.Net;
using Ext.Net.MVC;
using Sharp.DataTransferObject;
using Sharp.Infrastructure.WebApiClient;
using Sharp.Portal.ExtNet;
using Sharp.Portal.Mvc;
using Sharp.ServiceContracts;
using Sharp.WebBooter.Areas.Platform.Models;
using PartialViewResult = Ext.Net.MVC.PartialViewResult;

namespace Sharp.WebBooter.Areas.Platform.Controllers
{
    [Authorize]
    [AutomaticTemplate]
    public class JobController : Controller
    {
        // GET: Job
        public ActionResult Index(string containerId)
        {
            return new PartialViewResult
            {
                Model = new JobIndexModel(),
                ContainerId = containerId,
                RenderMode = RenderMode.AddTo
            };
        }

        /// <summary>
        /// 分页数据
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public ActionResult GetPaged(ExtRequestParameters parameters)
        {
            using (var proxy = new WebApiClientServiceProxy<IEnterpriseService>())
            {
                var pageInfo = proxy.Channel.GetJobPaged(parameters.ToPagingFeature());

                return this.Store(pageInfo.Data, pageInfo.TotalRecords);
            }
        }

        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            JobDTO dto;
            using (var proxy = new WebApiClientServiceProxy<IEnterpriseService>())
            {
                dto = proxy.Channel.GetJobInfo(id.Value);
            }

            var vm = dto.To<JobModel>();

            return new PartialViewResult { Model = vm };
        }

        public async Task<ActionResult> Create()
        {
            var vm = new JobModel();
            vm.ParentIdDataSource = await OrgController.OrgTree();
            return new PartialViewResult { Model = vm };
        }

        public async Task<ActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            JobDTO dto = null;
            using (var proxy = new WebApiClientServiceProxy<IEnterpriseService>())
            {
                dto = proxy.Channel.GetJobInfo(id.Value);
            }
            if (dto == null)
            {
                return HttpNotFound();
            }
            var vm = dto.To<JobModel>();
            vm.ParentIdDataSource = await OrgController.OrgTree();

            return new PartialViewResult { Model = vm };
        }

        [System.Web.Mvc.HttpPost]
        public ActionResult Save(JobModel user)
        {
            JobDTO dto = user.To<JobDTO>();

            using (var proxy = new WebApiClientServiceProxy<IEnterpriseService>())
            {
                var res = proxy.Channel.SaveJob(dto);

                return this.JsonNet(res);
            }
        }

        [System.Web.Mvc.HttpPost]
        public ActionResult Delete(int[] ids)
        {
            using (var proxy = new WebApiClientServiceProxy<IEnterpriseService>())
            {
                var res = proxy.Channel.DeleteJob(ids);

                return this.JsonNet(res);
            }
        }
    }
}